﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admins_Category : System.Web.UI.Page
{
    static string Id = "";
    static int Loai_tour_cha_id = 0;
    static bool Insert = false;
    static string Level = "";
    static string Lang = "";
    public static string ListName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            BindGrid();

        }
    }

    private void BindGrid()
    {
        LoaiTour loaiTour = new LoaiTour();
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        lst = loaiTour.getAllDanhMucTour();
        if (lst.Count > 0)
        {
            grid_loai_san_pham.DataSource = lst;
            grid_loai_san_pham.DataBind();
        }
    }




    protected void grdCategory_ItemDataBound(object sender, DataGridItemEventArgs e)
    {

    }

    protected void grdCategory_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grid_loai_san_pham.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void grdCategory_ItemCommand(object source, DataGridCommandEventArgs e)
    {

        string strCA = e.CommandArgument.ToString();
        LoaiTour loaiTour = new LoaiTour();
        ObjLoaiTour objLoaiTour;
        switch (e.CommandName)
        {
            case "Edit":
                Id = strCA;
                pnView.Visible = false;
                pnUpdate.Visible = true;
                
                objLoaiTour = loaiTour.getLoaiTourById(strCA);
                txt_ten_loai_san_pham.Text = objLoaiTour.Ten_loai_tour;
                txt_thu_tu.Text = objLoaiTour.Vi_tri.ToString();
                drop_loai_san_pham.DataSource = loaiTour.getAllDanhMucTour();
                drop_loai_san_pham.DataTextField = "ten_loai_tour";
                drop_loai_san_pham.DataValueField = "loai_tour_id";
                drop_loai_san_pham.DataBind();
                drop_loai_san_pham.Items.Insert(0, new ListItem("--chọn---", "0"));
                ListItem selecteditem = new ListItem();
                selecteditem = drop_loai_san_pham.Items.FindByValue(objLoaiTour.Loai_tour_cha_id.ToString());
                if (selecteditem != null)
                    selecteditem.Selected = true;

                if (objLoaiTour.Hien_danh_muc == "True")
                {
                    ck_hien_danh_muc.Checked = true;
                }
                Insert = true;
                break;

            case "Delete":
                objLoaiTour = new ObjLoaiTour();
                objLoaiTour.Loai_tour_id = int.Parse(strCA);
                loaiTour.deleteLoaiTourByTrangThai(objLoaiTour);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                break;

            case "Active":
                objLoaiTour = new ObjLoaiTour();
                string str = e.Item.Cells[2].Text;
                objLoaiTour.Loai_tour_id = int.Parse(strCA);
                if (str == Constants.ON)
                {
                    
                    objLoaiTour.Trang_thai = "0";
                    loaiTour.updateLoaiTourByTrangThai(objLoaiTour);   
                }
                else
                {
                    
                    objLoaiTour.Trang_thai = "1";
                    loaiTour.updateLoaiTourByTrangThai(objLoaiTour);
                }
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                break;
        }
        

    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        txt_ten_loai_san_pham.Text = "";
        txt_thu_tu.Text = "1";
        ck_hien_danh_muc.Checked = false;
        Insert = false;
        pnUpdate.Visible = true;
        pnView.Visible = false;
        LoaiTour loaiTour = new LoaiTour();
        drop_loai_san_pham.DataSource = loaiTour.getAllDanhMucTour();
        drop_loai_san_pham.DataTextField = "ten_loai_tour";
        drop_loai_san_pham.DataValueField = "loai_tour_id";
        drop_loai_san_pham.DataBind();
        drop_loai_san_pham.Items.Insert(0, new ListItem("--chọn---", "0"));
    }

    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        string sql = "";
        foreach (DataGridItem item in grid_loai_san_pham.Items)
        {
            CheckBox checkBox = (CheckBox)item.Cells[0].Controls[1];
            
            if (checkBox.Checked == true)
            {
                sql+="<LoaiTour Loai_san_pham_id=\""+item.Cells[1].Text+"\" ></LoaiTour>";
            }
        }
        LoaiTour loaiTour = new LoaiTour();
        loaiTour.deleteLoaiTourByXml(sql);
        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
    }

    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        Utils utils = new Utils();
        //check loi nhap so
        List<WebControl> lst = new List<WebControl>();
        lst.Add(txt_thu_tu);
        lb_messenger.Text= utils.checkDataType(lst);
        if (lb_messenger.Text == "")
        {
            LoaiTour loaiTour = new LoaiTour();
            ObjLoaiTour objLoaiTour = new ObjLoaiTour();
            objLoaiTour.Ten_loai_tour = txt_ten_loai_san_pham.Text;
            objLoaiTour.Hien_danh_muc = "0";
            objLoaiTour.Hien_trang_chu = 0;
            objLoaiTour.Vi_tri = int.Parse(txt_thu_tu.Text);
            objLoaiTour.Loai_tour_cha_id=int.Parse(drop_loai_san_pham.SelectedValue);
            if (ck_hien_danh_muc.Checked)
            {
                objLoaiTour.Hien_danh_muc = "1";
            }
            //update loai san pham
            if (Insert)
            {
                objLoaiTour.Loai_tour_id = int.Parse(Id);
                loaiTour.updateLoaiTour(objLoaiTour);
            }
                //them moi
            else
            {

                loaiTour.createLoaiTour(objLoaiTour);
            }

            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }

    }

    private void LoadFilterNewsAutocomplete()
    {

    }

    protected void Back_Click(object sender, EventArgs e)
    {
        pnView.Visible = true;
        pnUpdate.Visible = false;
        Level = "";
        Insert = false;
    }

    protected void Filter_Click(object sender, EventArgs e)
    {

    }

    protected void UnFilter_Click(object sender, EventArgs e)
    {

    }

    protected void lbtUpdateOrd_Click(object sender, EventArgs e)
    {
        string sql = "";
        foreach (DataGridItem item in grid_loai_san_pham.Items)
        {
            TextBox txt = (TextBox)item.FindControl("txtOrd");
            sql += "<LoaiTour Loai_san_pham_id=\"" + item.Cells[1].Text + "\" Vi_tri= \"" + txt.Text + "\" ></LoaiTour>";
            
        }
        LoaiTour loaiTour = new LoaiTour();
        loaiTour.updateViTriLoaiTourByXml(sql);

        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
    }

    protected void lbtUpdateOrdBottom_Click(object sender, EventArgs e)
    {

    }
 
}
